package org.example.mall.admin.service.marketing.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import org.elasticsearch.client.RestHighLevelClient;
import org.example.mall.admin.configuration.file.UrlsCallback;
import org.example.mall.admin.constant.es.enums.EsIndexEnum;
import org.example.mall.admin.mapper.marketing.ArticleMapper;
import org.example.mall.admin.model.entity.marketing.Article;
import org.example.mall.admin.model.es.marketing.ArticleEs;
import org.example.mall.admin.service.manage.SysFileService;
import org.example.mall.admin.service.marketing.ArticleService;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.function.Consumer;

/**
 * <p>
 * 文章管理 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2022-01-12
 */
@Service
@AllArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    private final ObjectMapper objectMapper;
    private final SysFileService fileService;
    private final RestHighLevelClient restHighLevelClient;
    private final ThreadPoolTaskExecutor applicationTaskExecutor;

    @Override
    public RestHighLevelClient restHighLevelClient() {
        return restHighLevelClient;
    }

    @Override
    public String index() {
        return EsIndexEnum.ARTICLE.getIndex();
    }

    @Override
    public ObjectMapper objectMapper() {
        return objectMapper;
    }

    @Override
    public ThreadPoolTaskExecutor applicationTaskExecutor() {
        return applicationTaskExecutor;
    }

    @Override
    public Boolean add(Article article) {
        UrlsCallback urlsCallback = fileService.replaceNetUrlsCallback(article.getContent());
        article.setContent(urlsCallback.getContent());
        article.setContentUrls(urlsCallback.getUrls());
        boolean save = save(article);
        fileService.asyncLinkFile(article);
        asyncSaveEs(article.getArticleId().toString(), article, ArticleEs.class);
        return save;
    }

    @Override
    public Boolean edit(Article article) {
        Article origin = getById(article.getArticleId());
        UrlsCallback urlsCallback = fileService.replaceNetUrlsCallback(article.getContent());
        article.setContent(urlsCallback.getContent());
        article.setContentUrls(urlsCallback.getUrls());
        article.setSyncEsStatus("0");
        boolean update = updateById(article);
        fileService.asyncEditLinkFile(origin, article);
        asyncUpdateEs(article.getArticleId().toString(), article, ArticleEs.class);
        return update;
    }

    @Override
    public Boolean del(Integer articleId) {
        Article origin = getById(articleId);
        boolean remove = removeById(articleId);
        fileService.asyncUnLinkFile(origin);
        asyncDeleteEs(articleId.toString());
        return remove;
    }


    @Override
    public IPage<Article> getPage(Page page, Article article) {
        return getBaseMapper().getPage(page, article);
    }

    @Override
    public Consumer<String> syncEsStatus() {
        return articleId -> {
            Article article = new Article();
            article.setArticleId(Integer.parseInt(articleId));
            article.setSyncEsStatus("1");//同步到ES中成功
            updateById(article);
        };
    }
}
